<?php
declare (strict_types=1);

namespace app\api\controller;

use app\api\controller\Base;
use app\common\model\Product as ProductModel;
use app\common\model\ProductCate;
use \think\Request;
use \think\facade\Db;
use app\common\model\Advert;

/**
 * Class Seckill 秒杀商城
 * @package app\index\controller
 */
class Seckill extends Base
{

    /**
     * 商城分类
     */
    public function shop_seckill_index(Request $request)
    {
        //一级
        $w = [];
        $w[] = ['status','=','1'];
        $w[] = ['pid','=','0'];
        $cate1 = ProductCate::getAllList($w,'id,title',"sort asc,id desc");
        $cc = [];
        $cc[0]['id'] = '0';
        $cc[0]['title'] = "全部";
        foreach($cate1 as $k=>$v){
            $cc[$k+1]['id'] = $v['id'];
            $cc[$k+1]['title'] = $v['title'];
        }
        $cate1 = $cc;
        //二级类
        $cate_id1 = $request->param("cate_id1");  //一级类id
        $ww = [];
        $ww[] = ['status','=',1];
        $whe= [];
        $whe[] = ['status','=',1];
        if(!empty($cate_id1)){
            $ww[] = ['pid','=',$cate_id1];
            $whe[] = ['cate_id1','=',$cate_id1];
        }else{
            $ww[] = ['pid','>',0];
        }
        $cate2 = ProductCate::getAllList($ww,'id,title,imgurl',"sort asc,id desc");
        $n = 0;
        $cc = [];
        foreach ($cate2 as $k => &$v) {
            $v['imgurl'] = imageUrl($v['imgurl']);
        }
        $cate2 = $cate2->toArray();
        $cate2 = array_chunk($cate2, 10);
        #首页轮播图
        $advert = Advert::field('imgurl')->where(['type' => 2])->order('sort desc,id desc')->select();
        foreach ($advert as &$advert_value) {
            $advert_value['imgurl'] = imageUrl($advert_value['imgurl']);
        }
        return $this->renderSuccess("请求成功",compact('cate1','cate2','advert'));
//        $user = $this->getUser();
//        $user_id = $user['id'];
//        #是否有分销人
//        $share_user_is = 0;
//        $goods_id = request()->param('goods_id/d', 0);
//        $good_info = Db::name('kk_product')->field('id')->where(['id' => $goods_id])->where(['status' => 0])->find();
////        if ($good_info) {
////            $share_user_id = request()->param('share_user_id/d', 0);
////            $share_user_id = $share_user_id -888;
////            if($share_user_id > 0){
////                $share_user = Db::name('user')->field('id')->where('id',$share_user_id)->find();
////                //  && $share_user_id != $user_id
////                if($share_user){
////                    $start_time = strtotime(date('Y-m-d',time()));
////                    $end_time = $start_time + 86399;
////                    $where_time = [];
////                    $where_time[] = ['addtime','BETWEEN',[$start_time,$end_time]];
////                    $shar_count = Db::name('kk_share')->field('id')->where('user_id',$user_id)->where($where_time)->count();
////                    if($shar_count < 3){
////                        $shar_info = Db::name('kk_share')->where('user_id',$user_id)->where('share_user_id',$user_id)->where('goods_id',$goods_id)->where($where_time)->find();
////                        if(!$shar_info){
////                            $share_user_is = 1;
////                            Db::name('kk_share')->insert([
////                                'user_id'=>$user_id,
////                                'goods_id'=>$goods_id,
////                                'share_user_id'=>$share_user_id,
////                                'addtime'=>time(),
////                            ]);
////                        }
////                    }else{
////                        $share_user_is = 2;
////                    }
////                }
////            }
////        }
//
//        #时间段
//        $time_data = Db::name('kk_seckill')
//            ->field('id,seckill_time')
//            ->order('seckill_time asc')
//            ->select()->toArray();
//        $new_data = $time_data;
////        $now_time = time();
////        foreach ($time_data as $key => &$value) {
////            $time_start_value = strtotime(date('Y-m-d ' . $value['seckill_time'], time()));
////            $time_end_value = $time_start_value + 3599;
////            if ($now_time < $time_start_value) {#未开始
////                $status = '未开始';
////                $status_is = 0;
////            } else if ($now_time >= $time_start_value && $now_time < $time_end_value) {#进行中
////                $status = '进行中';
////                $status_is = 1;
////            } else {#结束
////                $status = '已结束';
////                $status_is = 2;
////            }
////            $value['status'] = $status;
////            list($hour, $minute) = explode(":", $value['seckill_time']);
////            $value['seckill_time'] = $hour . ':' . $minute;
////            // if ($status_is != 2) {
////                $new_data[] = $value;
////            // }
////        }
//        return $this->renderSuccess("请求成功", $new_data);
    }

    /**
     * 商城秒杀时间段切换商品
     */
    public function shop_seckill_product(Request $request)
    {
        $title = $request->param("title");
        $cate_id2 = $request->param("cate_id2");
        $cate_id1 = $request->param("cate_id1");
        $w= [];
        if(!empty($title)){
            $w[] = ['title','like','%'.$title.'%'];
        }
         if(!empty($cate_id1)){
            $w[] = ['cate_id1','=',$cate_id1];
        }
        if(!empty($cate_id2)){
            $w[] = ['cate_id2','=',$cate_id2];
        }
        $w[] = ['status','=',0];

        $seckill_data =Db::name('kk_product')
            ->field('id,title,image')
            ->where(['status' => 0])
            ->where($w)
            ->order('sort desc,id desc')
            ->paginate(15);
        $data = $seckill_data->items();
        $last_page = $seckill_data->lastPage();
        foreach($data as $k=>&$v){
            #商品规格信息
            $spec_info = Db::name('kk_product_spec')->field('price,seckill_price,stock')
                ->where(['pro_id' => $v['id']])
                ->order('price asc')
                ->find();

            $v['price'] = $spec_info['price'];
            $v['image'] = imageUrl($v['image']);
        }
        #返回数据
        $newData = [
            'data' => $data,
            'last_page' => $last_page,
        ];
        return $this->renderSuccess("请求成功",$newData);
//        $user = $this->getUser();
//        $user_id = $user['id'];
//        $id = request()->param('id/d', 0);
//        $time_info = Db::name('kk_seckill')->where(['id' => $id])->find();
////        dd($time_info);
//        if (!$time_info) {
//            $time_info = Db::name('kk_seckill')
//                ->order('seckill_time asc')
//                ->find();
//            $id = $time_info['id'];
//            // $new_data = [];
//            // $now_time = time();
//            // foreach ($time_data as $key => &$value) {
//            //     $time_start_value = strtotime(date('Y-m-d ' . $value['seckill_time'], time()));
//            //     $time_end_value = $time_start_value + 3599;
//            //     if ($now_time < $time_start_value) {#未开始
//            //         $status_is = 0;
//            //     } else if ($now_time >= $time_start_value && $now_time < $time_end_value) {#进行中
//            //         $status_is = 1;
//            //     } else {#结束
//            //         $status_is = 2;
//            //     }
//            //     if ($status_is != 2) {
//            //         $id = $value['id'];
//            //         $time_info = $value;
//            //         break;
//            //     }
//            // }
//        }
//        #现在时间
////        $now_time = time();
////        $time_start_value = strtotime(date('Y-m-d ' . $time_info['seckill_time'], time()));
////        $time_end_value = $time_start_value + 3599;
////        if ($now_time < $time_start_value) {#未开始
////            $status = 0;
////        } else if ($now_time >= $time_start_value && $now_time < $time_end_value) {#进行中
////            $status = 1;
////        } else {#结束
////            $status = 2;
////        }
////        #限购数量
////        $goods_num = $time_info['goods_num'];
////        $where_time = [];
////        $where_time[] = ['addtime', 'BETWEEN', [$time_start_value, $time_end_value]];
//        #秒杀商品信息
//        $seckill_data = Db::name('kk_product')
//            ->field('id,title,image')
//            ->where(['status' => 0])
//            ->where(['seckill_id' => $id])
//            ->order('sort desc,id desc')
//            ->paginate(10);
//        $data = $seckill_data->items();
//        $last_page = $seckill_data->lastPage();
//
//        $start_time = strtotime(date('Y-m-d',time()));
//        $end_time = $start_time + 86399;
//        foreach ($data as &$item){
//            $shar_count = Db::name('kk_share')->field('id')->where('share_user_id',$user_id)->where('goods_id',$item['id'])->where('addtime','BETWEEN',[$start_time,$end_time])->count();
//            if($shar_count >= 3){
//                $status = 3;
//            }
//            #当前时间段购买商品数量
//            $buy_num = Db::name('kk_order_good')->field('goods_num')
//                ->where('goods_id','=',$item['id'])#商品ID
////                ->where($where_time)
//                ->sum('goods_num');
//            $item['buy_num'] = $buy_num;
////            #剩余
////            $surplus_num = ($buy_num) <= 0 ? 0 : ($buy_num);
////            $item['surplus_num'] = $surplus_num;
////            #总量
////            $item['all_num'] = $goods_num;
////            if ($surplus_num <= 0) {
////                $status = 4;#售馨
////            }
////            $pro = (float)bcdiv("$buy_num",2,2);
////            $item['pro'] = $pro * 100;
////            $item['status'] = $status;
//            #商品规格信息
//            $spec_info = Db::name('kk_product_spec')->field('price,seckill_price,stock')
//                ->where(['pro_id' => $item['id']])
//                ->order('price asc')
//                ->find();
//            $item['price'] = $spec_info['price'];
//            $item['seckill_price'] = $spec_info['seckill_price'];
//            $item['image'] = imageUrl($item['image']);
//        }
//
//        #返回数据
//        $newData = [
//            'data' => $data,
//            'last_page' => $last_page,
//        ];
//        return $this->renderSuccess("请求成功", $newData);
    }
    
    /**
     * 商品详情
     */
    public function shop_goods_detail()
    {

        $user = $this->getUser();
        $user_id = $user['id'];
        $goods_id = request()->param('goods_id/d', 0);
        $good_info = Db::name('kk_product')
            ->field('id,title,image,detail_image,content,sale_num,freight,spec_data')
            ->where(['id' => $goods_id])
            ->where(['status' => 0])
            ->find();
        if (!$good_info) {
            return $this->renderError("商品不存在或已下架");
        }

        #现在时间
        $now_time = time();
//        #秒杀倒计时=====================
//        $seckill_data = Db::name('kk_seckill')->field('seckill_time,goods_num')->order('id asc')->select();
//        $seckill_end_time = 0;
//        $seckill_status = 0;
//        foreach ($seckill_data as $key => $value) {
//            $time_start_value = strtotime(date('Y-m-d ' . $value['seckill_time'], time()));
//            $time_end_value = $time_start_value + 3599;
//            if ($now_time >= $time_start_value && $now_time < $time_end_value) {#进行中
//                $seckill_end_time = $time_end_value;
//                $seckill_status = 1;
//                break;
//            }
//        }
        

//        $seckill_surplus_time = ($seckill_end_time - time()) <= 0 ? 0 : $time_end_value;
//        $good_info['seckill_status'] = $seckill_status;
//        $good_info['seckill_surplus_time'] = $seckill_surplus_time;

        $detail_image = json_decode($good_info['detail_image'], true);
        $detail_image = array_map(function ($v) {
            return imageUrl($v);
        }, $detail_image);

        $good_info['image'] = imageUrl($good_info['image']);
        $good_info['detail_image'] = $detail_image ? $detail_image : $good_info['image'];
        $good_info['content'] = contentUrl($good_info['content']);
        #规则信息
        $spec_info = Db::name('kk_product_spec')->field('price,seckill_price')
            ->where(['pro_id' => $good_info['id']])
            ->order('price asc')
            ->find();
        $good_info['price'] = $spec_info['price'];
        $good_info['seckill_price'] = $spec_info['seckill_price'];
        $good_info['spec_data'] = json_decode($good_info['spec_data'], true);
        return $this->renderSuccess("请求成功", $good_info);

    }


    /**
     * 商品规格
     */
    public function shop_goods_detail_spec()
    {
        $pro_id = request()->param('goods_id/d', 0);
        $ks = request()->param('ks', 0);
        $info = Db::name('kk_product')->field('id,title,image')
            ->where(['id' => $pro_id])
            ->where(['status' => 0])
            ->find();
        if (!$info) {
            return $this->renderError("商品不存在或已下架");
        }
        $spec_info = Db::name('kk_product_spec')
            ->where(['pro_id' => $pro_id])
            ->where(['ks' => $ks])
            ->find();
        if ($spec_info) {
            $spec_info['pic'] = $spec_info['pic'] ? imageUrl($spec_info['pic']) : imageUrl($info['image']);
            $spec_info['goods_name'] = $info['title'];
            $type = 1;
        } else {
            $type = 0;
        }
        $data = [
            'type' => $type,
            'spec_info' => $spec_info,
        ];
        return $this->renderSuccess("请求成功", $data);
    }

    /**
     * 分享商品详情
     * @param $goods_id 商品id
     */
    public function shop_goods_share(){
        $user = $this->getUser();
        $user_id = $user['id'];
        $pro_id = request()->param('goods_id/d', 0);
        $info = Db::name('kk_product')->field('id')->where(['id' => $pro_id])->where(['status' => 0])->find();
        if (!$info) {
            return $this->renderError("商品不存在或已下架");
        }        
        $start_time = strtotime(date('Y-m-d',time()));
        $end_time = $start_time + 86399;
        $where_time = [];
        $where_time[] = ['addtime','BETWEEN',[$start_time,$end_time]];
        $data = Db::name('kk_share')->where('share_user_id',$user_id)->where('goods_id',$info['id'])->where($where_time)->limit(3)->select()->toArray();
        foreach ($data as $k => &$v){
            $user = Db::name('user')->field('nickname,headimg')->where('id',$v['user_id'])->find();
            $v['nickname'] = $user['nickname'];
            $v['headimg'] = $user['headimg'];
        }
        return $this->renderSuccess("请求成功", $data);
    }
    

    /**
     * 下单浏览
     * @param $goods_id 商品id
     */
    public function shop_goods_browse()
    {
        $user = $this->getUser();
        $goods_id = request()->param('goods_id/d', 0);#商品id
        $ks = request()->param('ks', 0);#规格键名
        $num = request()->param('num/d', 1);#数量
        $is_money = request()->param('is_money/d', 2);#余额抵扣
        $is_integral = request()->param('is_integral/d', 2);#Woo币抵扣
        
        if ($is_money != 1 && $is_money != 2) {
            return $this->renderError("请求参数错误");
        }
        if ($is_integral != 1 && $is_integral != 2) {
            return $this->renderError("请求参数错误2");
        }
        if (RegInt($num)) {
            return $this->renderError("商品数量选择错误");
        }
        #商品信息
        $info = Db::name('kk_product')
            ->field('id,image,title,freight')
            ->where(['id' => $goods_id])
            ->where(['status' => 0])
            ->find();
        if (!$info) {
            return $this->renderError("商品不存在或已下架");
        }
        #选择规格
        $spec_goods = Db::name('kk_product_spec')
            ->where(['pro_id' => $info['id']])
            ->where(['ks' => $ks])
            ->find();
        if (!$spec_goods) {
            return $this->renderError("规格选择错误");
        }
        
        $newData = [];
        $goods_info['goods_name'] = $info['title'];
        $goods_info['pic'] = $spec_goods['pic'] ? imageUrl($spec_goods['pic']) : imageUrl($info['image']);
        $goods_info['freight'] = (float)$info['freight'];#运费
        $goods_info['ks'] = $spec_goods['ks'];
        $goods_info['name'] = $spec_goods['name'];
        $goods_info['num'] = $num;#数量
        $goods_info['price'] = $spec_goods['price'];
        $goods_info['price'] = $spec_goods['price'];

        $seckill_price = $spec_goods['price'];#秒杀价
        $freight = $info['freight'];#运费
        $total = bcmul("$seckill_price", "$num", 2);#秒杀价*数量
        $total = bcadd("$total", "$freight", 2);#秒杀价*数量 + 运费
        $use_money = 0;
        $use_integral = 0;
        #余额抵扣
        if ($is_money == 1 && $total > 0) {
            if ($user['money'] >= $total) {
                $use_money = $total * 1;
                $total = 0;
            } else {
                $use_money = $user['money'];
                $total = bcsub("$total", "$use_money", 2);
            }
        }
        #Woo币抵扣
        if ($is_integral == 1 && $total > 0) {
            if ($user['integral'] >= $total) {
                $use_integral = $total;
                $total = 0;
            } else {
                $use_integral = $user['integral'];
                $total = bcsub("$total", "$use_integral", 2);
            }
        }
        #结果集
        $newData['goods'] = $goods_info;#商品
        $newData['pay_price'] = $total;#合计(商品金额)
        $newData['money'] = $user['money'];#余额
        $newData['use_money'] = $use_money;#余额抵扣
        $newData['integral'] = $user['integral'];#Woo币
        $newData['use_integral'] = $use_integral;#Woo币抵扣
        return $this->renderSuccess("请求成功", $newData);
    }

    /**
     * 秒杀商城下单
     * @param $goods_id 商品id
     */
    public function shop_order_buy()
    {
        $user = $this->getUser();
        $uid = $user['id'];
        $name = request()->param("name", '');
        $mobile = request()->param("mobile", '');
        $address = request()->param("address", '');
        $message = request()->param("message", '');
        $goods_id = request()->param('goods_id/d', 0);#商品id
        $ks = request()->param('ks', 0);#规格键名
        $num = request()->param('num/d', 1);#数量
        $is_money = request()->param('is_money/d', 2);#余额抵扣
        $is_integral = request()->param('is_integral/d', 2);#Woo币抵扣
        #收货地址
        if (!$name || !$mobile || !$address) {
            return $this->renderError("请选择收货地址");
        }
        if ($is_money != 1 && $is_money != 2) {
            return $this->renderError("请求参数错误");
        }
        if ($is_integral != 1 && $is_integral != 2) {
            return $this->renderError("请求参数错误2");
        }
        if (RegInt($num)) {
            return $this->renderError("商品数量选择错误");
        }
//        if (RegMobile($mobile)) {
//            // return $this->renderError("手机号非法");
//        }
        #商品信息
        $info = Db::name('kk_product')
            ->field('id,image,title,freight')
            ->where(['id' => $goods_id])
            ->where(['status' => 0])
            ->find();
        if (!$info) {
            return $this->renderError("商品不存在或已下架");
        }
        #选择规格
        $spec_goods = Db::name('kk_product_spec')
            ->where(['pro_id' => $info['id']])
            ->where(['ks' => $ks])
            ->find();
        if (!$spec_goods) {
            return $this->renderError("规格选择错误");
        }
//        dd(11);
//        $redis = new \Redis();
//        $redis->connect('127.0.0.1', 6379);
//        $key = "shop_order_buy".'_'.$goods_id;
//        $redis_key_info = $redis->get($key);
//        if($redis_key_info){
//            return $this->renderError("操作太快了");
//        }else{
//            $redis->set($key, 1,2);
//        }
//
        #判断库存
        if ($num > $spec_goods['stock']) {
            return $this->renderError("库存不足");
        }
//        $now_time = time();
//        #秒杀时间
//        $seckill_data = Db::name('kk_seckill')->field('seckill_time,goods_num')->order('id asc')->select();
//        $seckill_statrt_time = 0;
//        $seckill_end_time = 0;
//        $seckill_goods_num = 0;
//        $seckill_status = 0;
//        foreach ($seckill_data as $key => $value) {
//            $time_start_value = strtotime(date('Y-m-d ' . $value['seckill_time'], time()));
//            $time_end_value = $time_start_value + 3599;
//            if ($now_time >= $time_start_value && $now_time < $time_end_value) {#进行中
//                $seckill_statrt_time = $time_start_value;
//                $seckill_end_time = $time_end_value;
//                $seckill_goods_num = $value['goods_num'];
//                $seckill_status = 1;
//                break;
//            }
//        }
//        if ($seckill_status !== 1) {
//            return $this->renderError("当前时间段未开放秒杀");
//        }
//        $where_time = [];
//        $where_time[] = ['addtime', 'BETWEEN', [$seckill_statrt_time, $seckill_end_time]];
//        $where_time[] = ['goods_id', '=', $goods_id['id']];#商品ID
//        #当前时间段购买商品数量
//        $buy_num = Db::name('kk_order_good')->field('id')->where($where_time)->sum('goods_num');
//        $buy_num = $buy_num + $num;
//        if ($buy_num >= $seckill_goods_num) {
//            $surplus = $seckill_goods_num - $buy_num;
//            return $this->renderError('剩余秒杀数量' . $surplus);
//        }
//
//        #判断是否满足购买添加
//        $start_time = strtotime(date('Y-m-d',time()));
//        $end_time = $start_time + 86399;
//        $shar_count = Db::name('kk_share')->field('id')->where('share_user_id',$uid)->where('goods_id',$goods_id)->where('addtime','BETWEEN',[$start_time,$end_time])->count();
//        if($shar_count < 3){
//            return $this->renderError("不满足购买条件");
//        }
        
        ##秒杀价
        $seckill_price = $spec_goods['price'];
        ##运费
        $freight = $info['freight'];
        #商品金额 = #秒杀价*数量
        $total_price = bcmul("$seckill_price", "$num", 2);
        #实际支付 = 商品金额 + 运费
        $price = bcadd("$total_price", "$freight", 2);
        #余额抵扣
        $use_money = 0;
        #Woo币抵扣
        $use_integral = 0;
        #余额抵扣
        if ($is_money == 1 && $price > 0) {
            if ($user['money'] >= $price) {
                $use_money = $price * 1;
                $price = 0;
            } else {
                $use_money = $user['money'];
                $price = bcsub("$price", "$use_money", 2);
            }
        }
        #Woo币抵扣
        if ($is_integral == 1 && $price > 0) {
            if ($user['integral'] >= $price) {
                $use_integral = $price;
                $price = 0;
            } else {
                $use_integral = $user['integral'];
                $price = bcsub("$price", "$use_integral", 2);
            }
        }

        Db::startTrans();
        $res = [];
        #订单号
        $order_no = create_order_no();
        $save_data = [
            'user_id' => $uid,
            'order_no' => $order_no,
            'total_price' => $total_price,#商品金额
            'price' => $price,#支付金额
            'money' => $use_money,#余额抵扣
            'integral' => $use_integral,#Woo币抵扣
            'freight_price' => $freight,#运费
            'status' => 0,#0待付款 1待发货 2待收货 3确认收货
            'content' => $message,
            'addtime' => time(),
        ];
        #生成订单
        $res[] = $order_id = Db::name('kk_order')->insertGetId($save_data);
        #收货地址信息
        $res[] = Db::name('kk_order_send')->insert([
            'user_id' => $uid,
            'order_id' => $order_id,
            'shou_name' => $name,
            'shou_mobile' => $mobile,
            'shou_region' => $address,
        ]);
        #订单商品
        $res[] = Db::name('kk_order_good')->insert([
            'user_id' => $uid,
            'order_id' => $order_id,
            'goods_id' => $info['id'],
            'goods_name' => $info['title'],
            'goods_image' => $info['image'],
            'goods_spec_id' => $spec_goods['id'],
            'goods_spec' => $spec_goods['name'],
            'goods_price' => $spec_goods['price'],
            'goods_seckill_price' => $spec_goods['seckill_price'],
            'goods_num' => $num,
            'addtime' => time(),
        ]);
//        dd($this->app);
        $res_pay = new \app\api\controller\Pay($this->app);

        $data = $res_pay->pay($order_no,$info['title'],$user['openid'],7);
        $data = json_decode($data,true);
        if(isset($data['status'])){
            Db::rollback();
            return $this->renderError("网络故障，请稍后重试(支付参数错误)");
        }
        if($data['is_weixin'] == 2){
            $res = $data['res'];
            $new_data['is_weixin'] = $data['is_weixin'];
        }else{
            $res[] = 1;
            $new_data = $data;
        }
        if (resCheck($res)) {
            Db::commit();
            return $this->renderSuccess('购买成功',$new_data);
        } else {
            Db::rollback();
            return $this->renderError("购买失败,请刷新重试");
        }
    }
    
    
    /**
     * 秒杀商城订单
     * status 0待付款 1待发货 2待收货 3确认收货
     */
    public function seckill_order_list(){
        
        $user = $this->getUser();
        $uid = $user['id'];        
        $where = [];
        $status = request()->param('status/d', 0);
        if ($status == 1 || $status == 2 || $status == 3) {
            $where[] = ['status','=',$status];
        } else {
            $where[] = ['status','>=',1];
        }
        $data = Db::name('kk_order')
            ->field('id,order_no,total_price,price,money,money,freight_price,status,addtime')
            ->where(['user_id' => $uid])
            ->order('id desc')
            ->where($where)
            ->paginate(10);
        $list = $data->items();
        $last_page = $data->lastPage();

        foreach ($list as $key => &$value) {
            #状态
            if ($value['status'] == 1) {
                $status_name = '待发货';
            } else if ($value['status'] == 2) {
                $status_name = '待收货';
            } else if ($value['status'] == 3) {
                $status_name = '已完成';
            } else {
                $status_name = '未知';
            }
            #商品
            $goods_info = Db::name('kk_order_good')->where(['order_id' => $value['id']])->find();
            $goods_info['goods_image'] = imageUrl($goods_info['goods_image']);
            $value['status_name'] = $status_name;
            $value['goods_info'] = $goods_info;
            $value['pay_price'] = bcadd($value['total_price'],$value['freight_price'],2);;
        }            
        $newData = [
            'data' => $list,
            'last_page' => $last_page,
        ];
        return $this->renderSuccess("请求成功", $newData);
    
    }
    
    
    /**
     * 秒杀商城订单查看详情
     * status 0待付款 1待发货 2待收货 3确认收货
     */    
    public function seckill_order_detail(){
        $user = $this->getUser();
        $uid = $user['id'];                
        $id = Request()->param('id/d', 0);
        $order_info = Db::name('kk_order')->where('id',$id)->where('user_id',$uid)->find();
        if (!$order_info) {
            return $this->renderError("参数错误");
        }
        $goods_info = Db::name('kk_order_good')->where('order_id',$id)->where('user_id',$uid)->find();
        if(!$goods_info){
            return $this->renderError("参数错误1");
        }
        $send_info = Db::name('kk_order_send')->where('order_id',$id)->where('user_id',$uid)->find();
        if(!$send_info){
            return $this->renderError("参数错误2");
        }

        $newData = [
            'shou_name'=>$send_info['shou_name'],
            'shou_mobile'=>$send_info['shou_mobile'],
            'shou_region'=>$send_info['shou_region'],
            'goods_name'=>$goods_info['goods_name'],
            'goods_image'=> imageUrl($goods_info['goods_image']),
            'goods_num'=>$goods_info['goods_num'],
            'goods_spec'=>$goods_info['goods_spec'],
            'goods_price'=>$goods_info['goods_price'],
            'goods_seckill_price'=>$goods_info['goods_seckill_price'],
            'pay_price'=> bcadd($order_info['total_price'],$order_info['freight_price'],2),
            'freight_price'=>$order_info['freight_price'],
            'order_no'=>$order_info['order_no'],
            'pay_time'=>date('Y-m-d H:i:s',$order_info['pay_time']), 
        ];
        return $this->renderSuccess("请求成功", $newData);
    }    
    
    
    /**
     * 秒杀商城订单查看物流
     * status 0待付款 1待发货 2待收货 3确认收货
     */    
    public function seckill_order_logistics(){
        $user = $this->getUser();
        $uid = $user['id'];                
        $id = Request()->param('id/d', 0);
        $send_info = Db::name('kk_order_send')->where('order_id',$id)->where('user_id',$uid)->find();
        if (!$send_info) {
            return $this->renderError("参数错误");
        }
        $goods_info = Db::name('kk_order_good')->where('order_id',$id)->where('user_id',$uid)->find();
        if(!$goods_info){
            return $this->renderError("参数错误1");
        }
        
        $mobile = substr($send_info['shou_mobile'],-4);
        $wuliu = getcourier($send_info['delivery_no'].':'.$mobile,$send_info['delivery_name']);
        if($wuliu['code'] == 0){
            $data = $wuliu['result']['list'];    
            $status_name = $wuliu['result']['Now_Status'];
        }else{
            $data = [];
            $status_name = '暂无物流';
        }
        $newData = [
            'delivery_name'=>$send_info['delivery_name'],
            'delivery_no'=>$send_info['delivery_no'],
            'goods_image'=> imageUrl($goods_info['goods_image']),
            'goods_num'=>$goods_info['goods_num'],
            'status_name'=>$status_name,
            'data'=>$data,
        ];
        return $this->renderSuccess("请求成功", $newData);
    }
    
    
     /**
     * 秒杀商城订单确认收货
     * status 0待付款 1待发货 2待收货 3确认收货
     */    
    public function seckill_order_confirm(){
        $user = $this->getUser();
        $uid = $user['id'];                
        $id = Request()->param('id/d', 0);
        $order_info = Db::name('kk_order')->where('id',$id)->where('user_id',$uid)->find();
        if (!$order_info) {
            return $this->renderError("参数错误");
        }
        if($order_info['status'] == 3){
            return $this->renderError("请求重复操作");
        }
        if($order_info['status'] != 2){
            return $this->renderError("订单状态异常");
        }
        
        $res = Db::name('kk_order')->where('id',$id)->update(['status'=>3,'confirm_time'=>time()]);
        if($res){
            return $this->renderSuccess("收货成功");
        }else{
            return $this->renderError("收货失败");
        }
    }


}